package com.alongstudio.account.service.impl;

import java.util.HashMap;
import java.util.Map;

import org.springframework.stereotype.Service;

import com.alongstudio.account.commons.exception.BusinessException;
import com.alongstudio.account.entity.KeepAccountsDetail;
import com.alongstudio.account.service.IDetailsService;

@Service("DetailsService")
public class DetailsService extends BaseService implements IDetailsService {

	private String className = DetailsService.class.getName();

	@Override
	public Map findIncomeAndExpenditureDetail(String userId, int start, int pageSize) throws BusinessException {
		Map result = new HashMap();
//		try {
//			String hql = "from KeepAccountsDetail d where d.type=" + SystemConstant.KEEP_ACCOUNT_TYPE_INCOME
//					+ " or d.type=" + SystemConstant.KEEP_ACCOUNT_TYPE_EXPENDITURE;
//			hql += " and d.clientUser.id=" + userId;
//			hql += " order by d.happenedDate";
//			List<KeepAccountsDetail> list = dao.query(hql, start, pageSize);
//			result.put(SystemConstant.QUERY_RESULT_DATA, list);
//			String hqlCount = "select count(d.id) " + hql;
//			List l = dao.query(hqlCount);
//			int count = Integer.parseInt(l.get(0).toString());
//			result.put(SystemConstant.QUERY_RESULT_COUNT, count);
//		}
//		catch (Exception e) {
//			throw new BusinessException(className, e);
//		}
		return result;
	}

	@Override
	public Map findDetail(String userId, Byte type, int start, int pageSize) throws BusinessException {
		Map result = new HashMap();
//		try {
//			String hql = "from KeepAccountsDetail d where d.clientUser.id=" + userId;
//			if (type != null) {
//				hql += " and d.type=" + type;
//			}
//			hql += " order by d.happenedDate";
//			List<KeepAccountsDetail> list = dao.query(hql, start, pageSize);
//			result.put(SystemConstant.QUERY_RESULT_DATA, list);
//			String hqlCount = "select count(d.id) " + hql;
//			List l = dao.query(hqlCount);
//			int count = Integer.parseInt(l.get(0).toString());
//			result.put(SystemConstant.QUERY_RESULT_COUNT, count);
//		}
//		catch (Exception e) {
//			throw new BusinessException(className, e);
//		}
		return result;
	}

	@Override
	public boolean deleteDetail(String id) throws BusinessException {
		boolean isOk = false;
		try {
			dao.deleteByPk(KeepAccountsDetail.class, id);
			isOk = true;
		}
		catch (Exception e) {
			throw new BusinessException(className, e);
		}
		return isOk;
	}

	@Override
	public boolean updateDetail(KeepAccountsDetail detail) throws BusinessException {
		boolean isOk = false;
		try {
			dao.update(detail);
			isOk = true;
		}
		catch (Exception e) {
			throw new BusinessException(className, e);
		}
		return isOk;
	}

}
